﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wallpaper.Data.SeedData;
using Wallpaper.Models.Entities;

namespace Wallpaper.Data.Configurations
{
    public class RoleConfiguration : IEntityTypeConfiguration<Role>
    {
            public void Configure(EntityTypeBuilder<Role> builder)
            {
                // 表名配置
                builder.ToTable("roles");

                // 主键配置
                builder.HasKey(r => r.Id);

                // 字段配置
                builder.Property(r => r.Id)
                    .HasMaxLength(36)
                    .IsRequired()
                    .HasComment("角色主键ID");

                builder.Property(r => r.RoleName)
                    .IsRequired()
                    .HasMaxLength(50)
                    .HasColumnType("nvarchar(50)");

                builder.Property(r => r.RoleCode)
                    .IsRequired()
                    .HasMaxLength(50)
                    .HasColumnType("varchar(50)");

                builder.Property(r => r.Description)
                    .HasMaxLength(200)
                    .HasColumnType("nvarchar(200)");

                builder.Property(r => r.IsEnabled)
                    .IsRequired()
                    .HasDefaultValue(true);

                builder.Property(r => r.IsDeleted)
                    .IsRequired()
                    .HasDefaultValue(false);

                builder.Property(r => r.CreatedAt)
                    .IsRequired()
                    .HasDefaultValueSql("GETDATE()");

            builder.Property(m => m.UpdatedAt)
              .HasComment("更新时间");

            // 索引配置
            builder.HasIndex(r => r.RoleCode)
                    .IsUnique()
                    .HasDatabaseName("IX_Role_RoleCode");

                builder.HasIndex(r => r.RoleName)
                    .HasDatabaseName("IX_Role_RoleName");

                // 数据种子
                builder.HasData(RoleSeedData.GetSeedData());


        }
    }
}
